<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"emoh.xyz","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="命令分布图 镜像相关 搜索镜像 1docker search *image_name*  下载镜像 1docker pull *image_name*  查看镜像 12docker imagesdocker image ls  删除镜像 12docker rmi *image_name*docker image rm *image_name*  导出镜像 1docker save *image_n">
<meta property="og:type" content="article">
<meta property="og:title" content="容器常用操作">
<meta property="og:url" content="http://emoh.xyz/2020/05/25/2020-05-25-%E5%AE%B9%E5%99%A8%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/index.html">
<meta property="og:site_name" content="Just do it!">
<meta property="og:description" content="命令分布图 镜像相关 搜索镜像 1docker search *image_name*  下载镜像 1docker pull *image_name*  查看镜像 12docker imagesdocker image ls  删除镜像 12docker rmi *image_name*docker image rm *image_name*  导出镜像 1docker save *image_n">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512114627057-1589272553499.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512150614123-1589272560575.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512150633190-1589272562024.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512150805510-1589272563919.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512150831873-1589272566726.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512150850949-1589272568967.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/1328551-20181213140747902-888532965-1589272570727.png">
<meta property="og:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512150954304-1589272572894.png">
<meta property="article:published_time" content="2020-05-25T04:54:53.000Z">
<meta property="article:modified_time" content="2020-05-25T05:15:38.223Z">
<meta property="article:author" content="Emoh">
<meta property="article:tag" content="docker">
<meta property="article:tag" content="操作">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wenlixiao.gitee.io/md/pic/image-20200512114627057-1589272553499.png">

<link rel="canonical" href="http://emoh.xyz/2020/05/25/2020-05-25-%E5%AE%B9%E5%99%A8%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>容器常用操作 | Just do it!</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Just do it!</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://emoh.xyz/2020/05/25/2020-05-25-%E5%AE%B9%E5%99%A8%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/touxiang.png">
      <meta itemprop="name" content="Emoh">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Just do it!">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          容器常用操作
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-05-25 12:54:53 / 修改时间：13:15:38" itemprop="dateCreated datePublished" datetime="2020-05-25T12:54:53+08:00">2020-05-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/docker/" itemprop="url" rel="index"><span itemprop="name">docker</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="命令分布图"><a href="#命令分布图" class="headerlink" title="命令分布图"></a>命令分布图</h1><p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512114627057-1589272553499.png" alt="image-20200512114627057"></p>
<h1 id="镜像相关"><a href="#镜像相关" class="headerlink" title="镜像相关"></a>镜像相关</h1><ul>
<li><p>搜索镜像</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search *image_name*</span><br></pre></td></tr></table></figure>
</li>
<li><p>下载镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull *image_name*</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br><span class="line">docker image ls</span><br></pre></td></tr></table></figure>
</li>
<li><p>删除镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker rmi *image_name*</span><br><span class="line">docker image rm *image_name*</span><br></pre></td></tr></table></figure>
</li>
<li><p>导出镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker save *image_name* &gt; /save/image/path/name.tar.gz</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>导入镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker load &lt; /save/image/path/name.tar.gz</span><br></pre></td></tr></table></figure>
</li>
<li><p>提交创建的自定义镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit *container_id* *镜像名称*</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">[root@c7-dev ~]<span class="comment"># docker images</span></span><br><span class="line">REPOSITORY TAG IMAGE ID CREATED SIZE</span><br><span class="line">docker.io/centos latest 1e1148e4cc2c 6 days ago 202 MB</span><br><span class="line">docker.io/ubuntu latest 93fd78260bd1 3 weeks ago 86.2 MB</span><br><span class="line">docker.io/hello-world latest 4ab4c602aa5e 3 months ago 1.84 kB</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">6ecddae6e645 centos <span class="string">"bash"</span> 21 minutes ago Exited (0) About a minute ago boring_mestorf</span><br><span class="line"><span class="comment"># 进入容器，执行一些安装的过程省略</span></span><br><span class="line"><span class="comment"># 对容器进行一些自定制的操作后，提交这个容器生成新的镜像</span></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker commit 6ecdd gandoufu/centos-wget</span></span><br><span class="line">sha256:bac2d675a9c230f0a60b0af35b5d4e3cb337d7c6f122a05b60a9172ddb9fbda0</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">6ecddae6e645 centos <span class="string">"bash"</span> 24 minutes ago Exited (0) 4 minutes ago boring_mestorf</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker images</span></span><br><span class="line">REPOSITORY TAG IMAGE ID CREATED SIZE</span><br><span class="line">gandoufu/centos-wget latest bac2d675a9c2 16 seconds ago 328 MB</span><br><span class="line">docker.io/centos latest 1e1148e4cc2c 6 days ago 202 MB</span><br><span class="line">docker.io/ubuntu latest 93fd78260bd1 3 weeks ago 86.2 MB</span><br><span class="line">docker.io/hello-world latest 4ab4c602aa5e 3 months ago 1.84 kB</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将生成的新的镜像推送至远程仓库</span></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker login</span></span><br><span class="line">Username: gandoufu</span><br><span class="line">Password:</span><br><span class="line">Login Succeeded</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker images</span></span><br><span class="line">REPOSITORY TAG IMAGE ID CREATED SIZE</span><br><span class="line">gandoufu/centos-wget latest bac2d675a9c2 10 minutes ago 328 MB</span><br><span class="line">docker.io/centos latest 1e1148e4cc2c 6 days ago 202 MB</span><br><span class="line">docker.io/ubuntu latest 93fd78260bd1 3 weeks ago 86.2 MB</span><br><span class="line">docker.io/hello-world latest 4ab4c602aa5e 3 months ago 1.84 kB</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker tag bac2 gandoufu/centos7-wget:latest</span></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker images</span></span><br><span class="line">REPOSITORY TAG IMAGE ID CREATED SIZE</span><br><span class="line">gandoufu/centos-wget latest bac2d675a9c2 12 minutes ago 328 MB</span><br><span class="line">gandoufu/centos7-wget latest bac2d675a9c2 12 minutes ago 328 MB</span><br><span class="line">docker.io/centos latest 1e1148e4cc2c 6 days ago 202 MB</span><br><span class="line">docker.io/ubuntu latest 93fd78260bd1 3 weeks ago 86.2 MB</span><br><span class="line">docker.io/hello-world latest 4ab4c602aa5e 3 months ago 1.84 kB</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker push gandoufu/centos7-wget</span></span><br><span class="line">The push refers to a repository [docker.io/gandoufu/centos7-wget]</span><br><span class="line">f79b6b9e2fa6: Pushing [==================================================&gt;] 129.6 MB</span><br><span class="line">071d8bd76517: Pushed</span><br><span class="line"></span><br><span class="line">打标签时的注意事项：</span><br><span class="line">docker tag bac2 gandoufu/centos7-wget</span><br><span class="line">bac2 是镜像的id</span><br><span class="line">gandoufu/centos7-wget 镜像的名称必须是自己的id名称/镜像名称</span><br><span class="line">latest 默认版本号，可以不用加</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h1 id="容器相关"><a href="#容器相关" class="headerlink" title="容器相关"></a>容器相关</h1><ul>
<li><p>创建容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">docker create image_name</span><br><span class="line">docker create -t -i image_name bash</span><br><span class="line">docker run image_name</span><br><span class="line">docker run image_name /bin/<span class="built_in">echo</span> “haha”</span><br><span class="line">docker run -it -d image_name</span><br><span class="line">docker run -it -d –rm –name mydo image_name</span><br><span class="line"></span><br><span class="line">使用create创建的容器，在使用时需要先start对应的容器；</span><br><span class="line">使用run创建的容器，直接运行；如果指定的image_name不存在，则会去镜像仓库下载镜像，然后根据镜像创建容器。</span><br><span class="line"></span><br><span class="line">参数说明：</span><br><span class="line">i 进入交互模式</span><br><span class="line">t 创建一个虚拟终端</span><br><span class="line">d 后台运行</span><br><span class="line">rm 退出后自动删除容器</span><br><span class="line">name 创建的容器使用自定义的名字</span><br><span class="line">注：i和t参数一起指定（-it），在实际操作时发现只指定i参数时，attach到实例时会出现卡死的情况。</span><br></pre></td></tr></table></figure>
</li>
<li><p>进入容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker attach *container_id|container_name*</span><br><span class="line">根据容器的id或名字进入指定的容器；如果容器处于未运行状态，需要先启动容器docker start container_id</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 22 minutes ago Exited (0) 3 seconds ago hungry_franklin</span><br><span class="line">[root@c7-dev ~]<span class="comment">#</span></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker start aefe</span></span><br><span class="line"></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker attach aefe</span></span><br><span class="line"></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 20 minutes ago Up 16 seconds hungry_franklin</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用运行的容器执行命令</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">docker exec *container_id* command</span><br><span class="line">[root@c7-dev ~]# docker ps</span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">aefe7162ed16 centos &quot;bash&quot; 15 minutes ago Up 15 minutes hungry_franklin</span><br><span class="line">[root@c7-dev ~]# docker exec aefe date</span><br><span class="line">Wed Dec 12 14:04:12 UTC 2018</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看容器</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">docker psdocker ps -adocker ps -aq</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker run -it -d centos bash</span></span><br><span class="line">aefe7162ed163028c36741c26545dc0f1967ada0c985ce32cee5cf79666d2a3b</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker create -it --name myubun ubuntu</span></span><br><span class="line">3ba687940aed4a65bfc0656ea23ab627eab71ec4c3bd796ab107674503dd6f72</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 45 seconds ago Up 45 seconds hungry_franklin</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">3ba687940aed ubuntu <span class="string">"/bin/bash"</span> 9 seconds ago Created myubun</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 47 seconds ago Up 47 seconds hungry_franklin</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -aq</span></span><br><span class="line">3ba687940aed</span><br><span class="line">aefe7162ed16</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>停止容器</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">docker stop *container_id*</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 18 minutes ago Up 18 minutes hungry_franklin</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker stop aefe</span></span><br><span class="line"></span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>删除容器</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">docker rm *container_id*</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">3ba687940aed ubuntu <span class="string">"/bin/bash"</span> 16 minutes ago Created myubun</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 17 minutes ago Up 17 minutes hungry_franklin</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker rm myubun</span></span><br><span class="line">myubun</span><br><span class="line">[root@c7-dev ~]<span class="comment"># docker ps -a</span></span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">aefe7162ed16 centos <span class="string">"bash"</span> 17 minutes ago Up 17 minutes hungry_franklin</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h1 id="仓库相关"><a href="#仓库相关" class="headerlink" title="仓库相关"></a>仓库相关</h1><ul>
<li><p>登录/退出仓库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">docker login/<span class="built_in">logout</span></span><br><span class="line"></span><br><span class="line">[root@docker163 ~]<span class="comment"># docker login</span></span><br><span class="line">Login with your Docker ID to push and pull images from Docker Hub. If you don<span class="string">'t have a Docker ID, head over to https://hub.docker.com to create one.</span></span><br><span class="line"><span class="string">Username: xiaowenli</span></span><br><span class="line"><span class="string">Password:</span></span><br><span class="line"><span class="string">Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password</span></span><br><span class="line"><span class="string">[root@docker163 ~]# docker login</span></span><br><span class="line"><span class="string">Login with your Docker ID to push and pull images from Docker Hub. If you don'</span>t have a Docker ID, head over to https://hub.docker.com to create one.</span><br><span class="line">Username: xiaowenli</span><br><span class="line">Password:</span><br><span class="line">WARNING! Your password will be stored unencrypted <span class="keyword">in</span> /root/.docker/config.json.</span><br><span class="line">Configure a credential helper to remove this warning. See</span><br><span class="line">https://docs.docker.com/engine/reference/commandline/login/<span class="comment">#credentials-store</span></span><br><span class="line"></span><br><span class="line">Login Succeeded</span><br><span class="line">[root@docker163 ~]<span class="comment"># docker logout</span></span><br><span class="line">Removing login credentials <span class="keyword">for</span> https://index.docker.io/v1/</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h1 id="外部访问容器"><a href="#外部访问容器" class="headerlink" title="外部访问容器"></a>外部访问容器</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">先下载一个training&#x2F;webapp镜像，用于测试</span><br><span class="line">[root@c7-dev ~]# docker run -d -P training&#x2F;webapp python app.py</span><br><span class="line">e8c1832a9814d61c61faf3d472b40c1164cd410caad855415f3bc56668f0cc2a</span><br><span class="line">[root@c7-dev ~]# docker ps</span><br><span class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</span><br><span class="line">e8c1832a9814 training&#x2F;webapp &quot;python app.py&quot; 4 seconds ago Up 3 seconds 0.0.0.0:32768-&gt;5000&#x2F;tcp tender_cori</span><br><span class="line">[root@c7-dev ~]# docker port e8c</span><br><span class="line">5000&#x2F;tcp -&gt; 0.0.0.0:32768</span><br><span class="line">[root@c7-dev ~]# docker top e8c1</span><br><span class="line">UID PID PPID C STIME TTY TIME CMD</span><br><span class="line">root 1781 1767 0 10:19 ? 00:00:00 python app.py</span><br><span class="line"></span><br><span class="line">[root@c7-dev ~]# docker run -d -p 8000:5000 --name flask training&#x2F;webapp python app.py</span><br><span class="line">fef819097329bf7eb90fcf868a4a047b39e62b300c7e30105c38d1adf7fbf10e</span><br><span class="line">[root@c7-dev ~]# docker port fef8</span><br><span class="line">5000&#x2F;tcp -&gt; 0.0.0.0:8000</span><br><span class="line">通过webapp镜像创建运行容器，在容器内运行一个简单的web应用，端口为5000。</span><br><span class="line"></span><br><span class="line">参数说明：</span><br><span class="line">-P 会使用宿主机的一个随机端口映射到容器内的5000端口；外部访问ip+随机端口就可以访问容器内的外部应用</span><br><span class="line">-p 指定外部端口与内部端口映射关系。外部端口:内部端口</span><br></pre></td></tr></table></figure>



<h1 id="硬件资源相关"><a href="#硬件资源相关" class="headerlink" title="硬件资源相关"></a>硬件资源相关</h1><ul>
<li><p>创建容器的时候，限制内存</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@c7-dev ~]# docker run -it -d -m 100M --memory-swap -1 --name mycent centos</span><br><span class="line">32d70771e7c88b70caaca4c8e7428421a0906dde6358d77441104a6162032447</span><br><span class="line">[root@c7-dev ~]# docker stats --no-stream 32d7</span><br><span class="line">CONTAINER CPU % MEM USAGE &#x2F; LIMIT MEM % NET I&#x2F;O BLOCK I&#x2F;O PIDS</span><br><span class="line">32d7 0.00% 380 KiB &#x2F; 100 MiB 0.37% 648 B &#x2F; 648 B 0 B &#x2F; 0 B 1</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>更新内存限制</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">[root@c7-dev ~]# docker update -m 200M --memory-swap -1 32d7</span><br><span class="line"></span><br><span class="line">[root@c7-dev ~]#</span><br><span class="line">[root@c7-dev ~]#</span><br><span class="line">[root@c7-dev ~]# docker stats --no-stream 32d7</span><br><span class="line">CONTAINER CPU % MEM USAGE &#x2F; LIMIT MEM % NET I&#x2F;O BLOCK I&#x2F;O PIDS</span><br><span class="line">32d7 0.00% 380 KiB &#x2F; 200 MiB 0.19% 648 B &#x2F; 648 B 0 B &#x2F; 0 B 1</span><br><span class="line"></span><br><span class="line">[root@c7-dev ~]# docker update -m 400M 32d7</span><br><span class="line">Error response from daemon: Cannot update container 32d70771e7c88b70caaca4c8e7428421a0906dde6358d77441104a6162032447: Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time</span><br><span class="line"></span><br><span class="line">注意：指定限制内存大小并且设置 memory-swap 值为 -1，表示容器程序使用内存受限，而 swap 空间使用不受限制（宿主 swap 支持使用多少则容器即可使用多少。如果 –memory-swap 设置小于 –memory则设置不生效，使用默认设置）。</span><br><span class="line">docker run -it --rm -m 100M ubuntu-stress:latest &#x2F;bin&#x2F;bash</span><br><span class="line">如果不添加–memory-swap选项，则表示容器中程序可以使用100M内存和100Mswap内存，默认情况下，–memory-swap 会被设置成 memory 的 2倍。-m 为物理内存上限，而 –memory-swap 则是 memory + swap 之和，当压测值是 –memory-swap 上限时，则容器中的进程会被直接 OOM kill。</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h1 id="命令参考表"><a href="#命令参考表" class="headerlink" title="命令参考表"></a>命令参考表</h1><p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512150614123-1589272560575.png" alt="image-20200512150614123"></p>
<p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512150633190-1589272562024.png" alt="image-20200512150633190"></p>
<p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512150805510-1589272563919.png" alt="image-20200512150805510"></p>
<p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512150831873-1589272566726.png" alt="image-20200512150831873"></p>
<p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512150850949-1589272568967.png" alt="image-20200512150850949"></p>
<img src="http://wenlixiao.gitee.io/md/pic/1328551-20181213140747902-888532965-1589272570727.png" alt="img" style="zoom:200%;" />

<p><img src="http://wenlixiao.gitee.io/md/pic/image-20200512150954304-1589272572894.png" alt="image-20200512150954304"></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/docker/" rel="tag"># docker</a>
              <a href="/tags/%E6%93%8D%E4%BD%9C/" rel="tag"># 操作</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/2020/05/27/2019-07-17-ELK%206%20%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2/" rel="next" title="ELK 6 安装部署">
      ELK 6 安装部署 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#命令分布图"><span class="nav-number">1.</span> <span class="nav-text">命令分布图</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#镜像相关"><span class="nav-number">2.</span> <span class="nav-text">镜像相关</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#容器相关"><span class="nav-number">3.</span> <span class="nav-text">容器相关</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#仓库相关"><span class="nav-number">4.</span> <span class="nav-text">仓库相关</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#外部访问容器"><span class="nav-number">5.</span> <span class="nav-text">外部访问容器</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#硬件资源相关"><span class="nav-number">6.</span> <span class="nav-text">硬件资源相关</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令参考表"><span class="nav-number">7.</span> <span class="nav-text">命令参考表</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Emoh"
      src="/images/touxiang.png">
  <p class="site-author-name" itemprop="name">Emoh</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Emoh</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
